package id.brian;

import id.brian.GraphicObject.Speed;
import id.brian.GraphicObject.Coordinates;
import id.brian.GameThread;
import id.brian.GraphicObject;
import id.brian.FalldownActivity;

import java.util.LinkedList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

	private GameThread mThread;
	private LinkedList<GraphicObject> mGraphics;
	private GraphicObject mBall;
	int posY = 0;
	int counter = 0;
	int numberOfPlatforms = 7;
	int platformNumber;
	boolean checker = false;
	private LinkedList<Integer> randomSpaces = new LinkedList<Integer>();
	int diameter = 0;
	private final int spaceWidth = 70;
	int ballSpeed = 10;
	int scoreCount = 0;


	private FalldownActivity mFalldownActivity;

	public Panel(FalldownActivity context) {
		super(context);
		getHolder().addCallback(this);
		setFocusable(true);
		mThread = new GameThread(getHolder(), this);
		mFalldownActivity = context;
	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		Coordinates coords;
		for (GraphicObject go: mGraphics) {
			Bitmap bitmap;
			bitmap = go.getBitmap();
			coords = go.getCoordinates();
			canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		}
		Bitmap bitmapBall;
		bitmapBall = mBall.getBitmap();
		Coordinates coordsBall;
		coordsBall = mBall.getCoordinates();
		canvas.drawBitmap(bitmapBall,coordsBall.getX(), coordsBall.getY(), null);
	}

	public void updatePhysics() {

		//Coordinates from the top rectangle
		Coordinates coords = mGraphics.get(0).getCoordinates();

		//Y position of the top rectangle
		posY = coords.getY();

		if (posY < 0) {
			//Remove top rectangle
			GraphicObject firstSpot = mGraphics.get(0);
			mGraphics.remove(0);
			randomSpaces.remove(0);

			//Add new rectangle 
			int rectWidth = getWidth();
			int rectHeight = rectWidth/24;
			int rand = (int) (Math.random()*((rectWidth) - spaceWidth));
			randomSpaces.addLast(rand); 
			// add gap to list
			int[] colors = new int[rectWidth * rectHeight];
			for (int j = 0; j < colors.length; j++) {
				int col = j % getWidth();
				if (!((col < rand + spaceWidth) && (col >= rand))){ 
					colors[j] = Color.GREEN;
				}
				else {
					colors[j] = Color.BLACK;
				}
			}

			Bitmap rectangle = Bitmap.createBitmap(colors, rectWidth, rectHeight, Bitmap.Config.ARGB_8888);
			GraphicObject platform = new GraphicObject(rectangle);
			firstSpot = platform;
			firstSpot.getCoordinates().setX(0);
			firstSpot.getCoordinates().setY(getHeight());
			mGraphics.addLast(firstSpot);
			//when the top platform is deleted, the platform that the ball is on moves up one
			platformNumber = platformNumber - 1;
		} 

		//move platforms up
		for (int i = 0;i<numberOfPlatforms;i++) {
			coords = mGraphics.get(i).getCoordinates();
			Speed speed = mGraphics.get(i).getSpeed();
			posY = coords.getY() + speed.getY();
			coords.setY(posY);
		}

		//gets coords and speed of ball
		Coordinates coordsBall = mBall.getCoordinates();
		Speed speedBall = mBall.getSpeed();

		//update ball position
		int posXball = coordsBall.getX() + speedBall.getX();
		int posYball = coordsBall.getY() + speedBall.getY();
		if ((posXball >= 0) && (posXball + mBall.getBitmap().getWidth()<=getWidth())) {
			coordsBall.setX(posXball);   
		}
		else if (posXball - diameter * 0.5<0) {
			coordsBall.setX(getWidth()-mBall.getBitmap().getWidth());
		}
		else if (posXball+mBall.getBitmap().getWidth()>getWidth()) {
			coordsBall.setX(0);
		}
		coordsBall.setY(posYball);
		Log.i("posXball",""+posXball);

		//lose
		if (posYball < 0) {
			mFalldownActivity.updateScores();
			mGraphics.clear();
			randomSpaces.clear();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			createNewGame();
		}

		//check if ball will fall through a hole
		if (platformNumber >=0) {
			if ((coordsBall.getX() + diameter<= randomSpaces.get(platformNumber) + spaceWidth) && (coordsBall.getX() >= randomSpaces.get(platformNumber)) 
					&& (speedBall.getX()<0)) {
				speedBall.setY(3);
				checker = true;
			}

			//check if ball will fall through a hole
			if ((coordsBall.getX()<= randomSpaces.get(platformNumber) + spaceWidth) && (coordsBall.getX()>= randomSpaces.get(platformNumber))) {
				speedBall.setY(3);
				checker = true;
			}
		}

		//stop falling
		if (platformNumber+1 < mGraphics.size() && (checker == true)) {
			if (coordsBall.getY() + mBall.getBitmap().getHeight()>= mGraphics.get(platformNumber +1).getCoordinates().getY()) {
				speedBall.setY(-4);
				checker = false;
				platformNumber= platformNumber + 1;
				scoreCount++;
				mFalldownActivity.Score(scoreCount);
				counter = 0;
			}
		}
	}

	public void createNewGame() {
		scoreCount = 0;
		mFalldownActivity.resetScores();
		mGraphics = new LinkedList<GraphicObject>();
		platformNumber = 3;
		for (int i = 0;i<numberOfPlatforms;i++) {
			int rectWidth = getWidth();
			int rectHeight = rectWidth/24;
			int rand = (int) (Math.random()*(rectWidth - spaceWidth));
			randomSpaces.add(rand);
			int[] colors = new int[rectWidth * rectHeight];
			for (int j = 0; j < colors.length; j++) {
				int col = j % getWidth();
				if (!((col < rand + spaceWidth) && (col > rand - 1))){ 
					colors[j] = Color.GREEN;
				}
			}
			Bitmap rectangle = Bitmap.createBitmap(colors, rectWidth, rectHeight, Bitmap.Config.ARGB_8888);
			GraphicObject platform = new GraphicObject(rectangle);
			platform.getCoordinates().setX(0);
			platform.getCoordinates().setY((getHeight()/numberOfPlatforms)*(i+1));
			mGraphics.add(platform);
		}
		diameter = getWidth()/24;
		int [] colors = new int[diameter*diameter];
		for (int i = 0; i < colors.length; i++) {
			int x = i / diameter - diameter/2;
			int y = i % diameter - diameter/2;
			if (x*x + y*y <= (diameter/2)*(diameter/2))
				colors[i] = Color.WHITE;
			else
				colors[i] = Color.TRANSPARENT;
		}
		Bitmap ball = Bitmap.createBitmap(colors, diameter, diameter, Bitmap.Config.ARGB_8888);//
		mBall = new GraphicObject(ball);
		//puts ball on platform number 4
		mBall.getCoordinates().setY(((getHeight()/numberOfPlatforms)*5)-diameter);
		//mBall.getCoordinates().setX(getWidth() - getWidth()/10);
		mBall.getCoordinates().setX(1);
		mBall.getSpeed().setX(0);
		mBall.getSpeed().setY(-4);
	}

	public void setBallSpeed (int speed) {
		if (mBall != null) {
			mBall.getSpeed().setX(speed*ballSpeed);
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	public void surfaceCreated(SurfaceHolder holder) {
		createNewGame();
		mThread.setRunning(true);
		mThread.start();

	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		mThread.setRunning(false);
		while (retry) {
			try {
				mThread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}
}